<?php
namespace Common\Model;
use Think\Model;

class MemberModel extends Model{
	
	private $table = "member"; //指定表名
	
	/**
	 * 获得一条数据
	 */
	public function  getOneData(){
		$data = I('post.','','htmlspecialchars');
		$getData = $this->where(array('username'=>$data['user'],'password'=>md5($data['pwd'])))->find();
		return $getData;
	} 
	
	/**
	 * 获得所有的数据
	 */
	public function getAllData(){
		$data = I('post.');
		$page = $data['page'];
		$pageSize = $data['rows'];
		$first = $pageSize * ($page-1);
		$order = $data['order'];
		$sort = $data['sort'];
		//获得总条数
		$total = $this->count();
		//获得数据
		$getData = $this->field('member_id,username,email,phone,auth,create_time')->order(" member_id $order " )->limit("$first,$pageSize")->select();
//		var_dump($this->getLastSql());
		$allData['total'] = $total;
		$allData['rows'] = $getData ;
		return $allData; 
	}
	/**
	 * 添加成功的数据
	 */
	public function updateSuccessData($id){
		$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
		$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
		$degree = $this->field('login_degree,login_time')->where(array('member_id'=>$id))->find();
		$data = array(
			'login_ip' => $user_IP,
			'login_degree' => $degree['login_degree']+1,
			'login_time' => date('Y-m-d H:i:s',time()),
			'login_next' => $degree['login_time']
		);	
		$this->where(array('member_id'=>$id))->save($data);	
	}
	/**
	 * 添加数据
	 */
	public function addData(){
		$postData = I('post.');
		$postData['password'] = md5($postData['password']);
		$postData['create_time'] = date('Y-m-d',time());
		$id = $this->data($postData)->add();
		return $id;
	}
	/**
	 * 获取一条信息 
	 */
	public function selectOneData($uid){
		$getData = $this->where(array('member_id'=>$uid))->find();
		return $getData;
	}
	/**
	 * 修改数据
	 */
	public function updateData(){
		$postData = I('post.');
		$postData['password'] = md5($postData['password']);
		$rows = $this->where(array('member_id'=>$postData['id']))->save($postData);
//		var_dump($this->getLastSql());
		return $rows;
	}
	/**
	 * 删除数据
	 */
	public function delData(){
		$id = I('post.id');
		$rows = $this->where(array('member_id'=>$id))->delete();
		return $rows;
	}
	

	
} 